/* 登录与注册 */
//登录
function login() {
    let mockData = Mock.mock({});
    new Vue({
        el: "#login",
        data() {
            return {
                loginshow: 0, //0为手机号登录 1为微信登录
                loginUser: {
                    loginName: '',
                    loginPassword: '',
                }
            }
        },
        methods: {
            getData() {

            },
            clickloginshow(n) {
                this.loginshow = n;
            },
            //点击登录
            loginButton: function() {
                sessionStorage.setItem('loginName', this.loginUser.loginName);
                axios({
                    method: 'post',
                    url: urlParam.domain() + '/api/login',
                    responseType: 'stream',
                    data: formartPostParms(this.loginUser),
                }).then(function(r) {
                    if (r.data.code == 1) {
                        sessionStorage.setItem('user', JSON.stringify(r.data.data));
                        sessionStorage.setItem('token', JSON.stringify(r.data.data.token));
                        //cookie 维持用户登录
                        globalAPI.setCookie("user", r.data.data, 5);
                        globalAPI.setCookie("token", r.data.data.token, 5);
                        //判断 是否跳转到首页 judgeUrl
                        if (sessionStorage.getItem("beforeUrl")) {
                            window.location.href = sessionStorage.getItem("beforeUrl");
                            //location.replace(sessionStorage.getItem("beforeUrl"))
                        } else {
                            window.location.href = '../../html/index/index.html';
                        }

                    } else {
                        alert(r.data.message);
                    }
                });
            }
        },
        created() {
            //如果登录直接跳转首页
            if (userLogin.info()) {
                window.location.href = '../../html/index/index.html';
            }
            this.getData();
        },
        mounted() {

        },
        components: {}
    });
}

//注册
function res() {
    let mockData = Mock.mock({});
    new Vue({
        el: "#res",
        data() {
            return {
                resagree: 0, //是否同意 用户注册协议和隐私注册 初始为0，不同意 1为同意
                codeButton: '获取验证码',
                timer: 60,
                registerInfo: {
                    loginPassword: '',
                    reloginPassword: '', //重复密码
                    telephone: '',
                    userName: '',
                    verificationCode: '',
                }
            }
        },
        methods: {
            getData() {
                // this.quan = mockData.quan;
            },
            clickresagree() {
                this.resagree == 0 ? this.resagree = 1 : this.resagree = 0;
            },
            //发送验证码
            sendCode: function(e) {
                var that = this;
                if (this.codeButton != '获取验证码') {
                    return false;
                }
                if (this.registerInfo.telephone == '') {
                    alert('手机号码不能为空！');
                    return false;
                }
                if (!(/^1[345678][0-9]{9}$/.test(this.registerInfo.telephone))) {
                    alert('手机号码格式不正确！');
                    return false;
                }
                that.codeButton = that.timer + '秒';
                axios({
                        method: 'get',
                        url: urlParam.domain() + '/api/sms/sendVerifyCode?telephone=' + this.registerInfo.telephone,
                        responseType: 'stream'
                    })
                    .then(function(r) {
                        if (r.data.code == 0) {
                            alert(r.data.message);
                        } else {
                            var intv = setInterval(function() {
                                that.timer--;
                                if (that.timer <= 0) {
                                    clearInterval(intv);
                                    that.codeButton = '获取验证码';
                                } else {
                                    that.codeButton = that.timer + '秒';
                                }
                            }, 1000)
                        }
                    })
            },
            //发送注册信息
            register: function() {
                var info = this.registerInfo;
                if (this.resagree == 0) {
                    alert('请阅读注册协议！');
                    return false;
                }
                if (info.userName == '') {
                    alert('用户名不能为空！');
                    return false;
                }
                if (info.reloginPassword == '' || info.loginPassword == '') {
                    alert('密码不能为空！');
                    return false;
                }
                if (info.reloginPassword != info.loginPassword) {
                    alert('两次密码不一致！');
                    return false;
                }
                if (info.telephone == '') {
                    alert('手机号码不能为空！');
                    return false;
                }
                if (!(/^1[345678][0-9]{9}$/.test(info.telephone))) {
                    alert('手机号码格式不正确！');
                    return false;
                }
                if (info.verificationCode == '') {
                    alert('验证码不能为空！');
                }
                axios({
                        method: 'post',
                        url: urlParam.domain() + '/api/register',
                        data: formartPostParms(info),
                        responseType: 'stream'
                    })
                    .then(function(response) {
                        if (response.data.code == 0) {
                            alert(response.data.message);
                        } else {
                            window.location.href = 'login.html';
                        }
                    })
            }
        },
        created() {
            this.getData();
        },
        mounted() {

        },
        components: {}
    });
}